package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeSchemaBo;
import org.dromara.knowledge.domain.vo.KnowledgeSchemaVo;
import org.dromara.knowledge.service.KnowledgeSchemaService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库模式控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/schema")
public class KnowledgeSchemaController extends BaseController {

    private final KnowledgeSchemaService knowledgeSchemaService;

    /**
     * 查询知识库模式列表
     */
    @SaCheckPermission("knowledge:schema:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeSchemaVo> list(KnowledgeSchemaBo bo, PageQuery pageQuery) {
        return knowledgeSchemaService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库模式列表
     */
    @SaCheckPermission("knowledge:schema:export")
    @Log(title = "知识库模式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeSchemaBo bo, HttpServletResponse response) {
        List<KnowledgeSchemaVo> list = knowledgeSchemaService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库模式数据", KnowledgeSchemaVo.class, response);
    }

    /**
     * 获取知识库模式详细信息
     *
     * @param schemaId 模式ID
     */
    @SaCheckPermission("knowledge:schema:query")
    @GetMapping("/{schemaId}")
    public R<KnowledgeSchemaVo> getInfo(@PathVariable Long schemaId) {
        return R.ok(knowledgeSchemaService.queryById(schemaId));
    }

    /**
     * 新增知识库模式
     */
    @SaCheckPermission("knowledge:schema:add")
    @Log(title = "知识库模式", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeSchemaBo bo) {
        return toAjax(knowledgeSchemaService.insertByBo(bo));
    }

    /**
     * 修改知识库模式
     */
    @SaCheckPermission("knowledge:schema:edit")
    @Log(title = "知识库模式", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeSchemaBo bo) {
        return toAjax(knowledgeSchemaService.updateByBo(bo));
    }

    /**
     * 删除知识库模式
     *
     * @param schemaIds 模式ID串
     */
    @SaCheckPermission("knowledge:schema:remove")
    @Log(title = "知识库模式", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schemaIds}")
    public R<Void> remove(@PathVariable Long[] schemaIds) {
        return toAjax(knowledgeSchemaService.deleteWithValidByIds(List.of(schemaIds), true));
    }

    /**
     * 根据ID查询模式信息
     *
     * @param id ID
     */
    @SaCheckPermission("knowledge:schema:query")
    @GetMapping("/id/{id}")
    public R<KnowledgeSchemaVo> getById(@PathVariable Long id) {
        return R.ok(knowledgeSchemaService.queryById(id));
    }

    /**
     * 根据模式组ID查询模式列表
     *
     * @param schemaGroupId 模式组ID
     */
    @SaCheckPermission("knowledge:schema:query")
    @GetMapping("/group/{schemaGroupId}")
    public R<List<KnowledgeSchemaVo>> getBySchemaGroupId(@PathVariable Long schemaGroupId) {
        return R.ok(knowledgeSchemaService.queryListBySchemaGroupId(schemaGroupId));
    }

    /**
     * 根据编码查询模式信息
     *
     * @param code 编码
     */
    @SaCheckPermission("knowledge:schema:query")
    @GetMapping("/code/{code}")
    public R<KnowledgeSchemaVo> getByCode(@PathVariable String code) {
        return R.ok(knowledgeSchemaService.queryByCode(code));
    }

    /**
     * 根据表名查询模式信息
     *
     * @param tableName 表名
     */
    @SaCheckPermission("knowledge:schema:query")
    @GetMapping("/table/{tableName}")
    public R<KnowledgeSchemaVo> getByTableName(@PathVariable String tableName) {
        return R.ok(knowledgeSchemaService.queryByTableName(tableName));
    }
}